import { Component, OnDestroy, OnInit } from '@angular/core'
import { INewPageColumn } from '@shared/interfaces/common'
import { AcquirerNewQrcodeChannelComponent } from '../../new/qrcode-channel/qrcode-channel.component'
import { ComWindow } from '@shared/classes/ComWindowClass'
import { NzMessageService } from 'ng-zorro-antd'
import { ActivatedRoute, Router } from '@angular/router'
import { AcquirerStoreService } from '../../new/acquirer-store.service'
import { CommonAcquirerService } from '@shared/services/acquirer.service'
import { PageService } from '@shared/services/page.service'
import { deepCopy } from '@delon/util'
import { Acquirer, WXChannel, ALPChannel, UPIChannel } from '@shared/interfaces/acquirer'
import { AcquirerService } from '../../acquirer.service'
import { Location } from '@angular/common'
import { encryptPassword } from 'app/utils/helpers'
import { I18NService } from '@core'
import { ECategoryUrl, ECategory } from '@shared/interfaces/trade'

@Component({
  selector: 'app-edit-qr-channel',
  templateUrl: './qr-channel.component.html',
})
export class AcquirerAddQrChannelComponent implements OnInit, OnDestroy {

  pages: INewPageColumn[] = []

  constructor(
    public msg: NzMessageService,
    public router: Router,
    public route: ActivatedRoute,
    public location: Location,
    public acquirerService: AcquirerService,
    public acquirerStoreService: AcquirerStoreService,
    public commonAcquirerService: CommonAcquirerService,
    public pageService: PageService,
    private i18n: I18NService,
  ) {}

  ngOnInit(): void {
    this.initData()
    this.initColumns()
    this.pageService.setChannelAdd(true)
  }

  ngOnDestroy(): void {
    /** 重置 channel edit */
    this.pageService.setChannelAdd(false)
    this.acquirerStoreService.reset()
  }

  initData(): void {
    const viewingAcquirer = this.commonAcquirerService.viewingAcquirer
    const cloneAcquirer: Acquirer = new Acquirer()
    Object.assign(cloneAcquirer, viewingAcquirer)
    cloneAcquirer.qrChannels = []
    this.acquirerStoreService.updateValue(cloneAcquirer)
  }

  initColumns(): void {
    this.pages = [
      {
        component: new ComWindow(AcquirerNewQrcodeChannelComponent, { title: this.trans('BIZ.ACQUIRER.TITLE.ADD_QR_CHANNEL') }),
      }
    ]
  }

  submit(): void {
    const acquirer = this.acquirerStoreService.value
    const channelToAdd = acquirer.qrChannels[0] as WXChannel | ALPChannel | UPIChannel// 每次只能加一条
    this.encryptSignKey(channelToAdd) // 对 signKey 进行加密

    const insCode = this.route.snapshot.params['id']
    const { id } = channelToAdd
    if (id) {
      /** 绑定已有的 */
      this.acquirerService.updateChannelForAcquirer(ECategoryUrl.qr, insCode, id, channelToAdd)
      .subscribe(
        isSuccess => {
          this.msg.success(this.trans('GENERAL.NOTIFICATION.ADD_SUCCESS'))
          this.location.back()
        },
        err => {
          this.msg.error(this.trans('GENERAL.NOTIFICATION.ADD_FAIL'))
        }
      )
      return
    }
    this.acquirerService.createChannelForAcquirer(ECategoryUrl.qr, insCode, channelToAdd).subscribe(
      isSuccess => {
        if (isSuccess) {
          this.msg.success(this.trans('GENERAL.NOTIFICATION.ADD_SUCCESS'))
          this.location.back()
        }
      },
      (err) => {
        const { error } = err
        this.msg.error(error.msg)
      }
    )
  }

  private encryptSignKey(channelToAdd: WXChannel| ALPChannel | UPIChannel): void {
    if (channelToAdd.signKey) {
      channelToAdd.signKey = encryptPassword(channelToAdd.signKey)
    }
  }

  private trans(str: string, params?: Object): string {
    return this.i18n.fanyi(str, params)
  }
}
